package gewu.suleju.sevice.imp;

import gewu.suleju.dao.iml.IHouseInforOneDao;
import gewu.suleju.model.vo.HouseInforAll;
import gewu.suleju.model.vo.PageInfoAll;
import gewu.suleju.model.vo.PageModel;
import gewu.suleju.sevice.IHouseInforAllService;
import gewu.suleju.sevice.ISearchHouseInforAllService;
import gewu.suleju.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Transactional
@Service
public class SearchHouseInforAllServiceImp implements ISearchHouseInforAllService {
    @Autowired
    private StringOperateUtil stringOperateUtil;

    @Autowired
    private IHouseInforOneDao houseInforOneDao;

    @Autowired
    private GetHouseInforAllUtil getHouseInforAllUtil;

    @Autowired
    private IHouseInforAllService houseInforAllService;

    @Autowired
    private ListForOverlapUtil listForOverlapUtil;

    @Autowired
    private PageInfoAll pageInfoAll;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void findHouseInforAllPage(String selectValue, String regionOrSubway, String checkboxsValue, Integer pageSize) {
        List<HouseInforAll> houseInforAllList = findHouseInforForThree(selectValue.trim(), regionOrSubway.trim(), checkboxsValue.trim());

        if(houseInforAllList.size() != 0) {
            PageModel<HouseInforAll> pageModel = new PageModel<>(houseInforAllList, pageSize);

            for (int i = 0; i < pageModel.getTotalPages(); i++) {

                List<HouseInforAll> sublist = new ArrayList<>();
                sublist.addAll(pageModel.getObjects(i + 1));

                pageInfoAll.setDataInforAll(sublist);
                pageInfoAll.setPageCount(pageModel.getTotalPages());
                pageInfoAll.setPageNum((i + 1));
                pageInfoAll.setPageSize(pageSize);
                pageInfoAll.setPageTotal(pageModel.getTotalRows());

                redisTemplate.opsForValue().set(i + 1+"", pageInfoAll);
            }
        } else {
            pageInfoAll.setDataInforAll(houseInforAllList);
            pageInfoAll.setPageCount(1);
            pageInfoAll.setPageNum(1);
            pageInfoAll.setPageSize(0);
            pageInfoAll.setPageTotal(0);

            redisTemplate.opsForValue().set(1+"", pageInfoAll);
        }
    }

    @Override
    public List<HouseInforAll> findHouseInforAll(String formFront) {
        List<HouseInforAll> houseInforAllList = new ArrayList<>();

        for(int i = 0;i< stringOperateUtil.stringIncise(formFront.trim()," ").length; i++) {
            String formFrontPart = stringOperateUtil.stringIncise(formFront.trim()," ")[i].trim();

            if (stringOperateUtil.isNumeric(formFrontPart.trim())) {
                if (houseInforOneDao.findByRentPriceAndIsStatus(stringOperateUtil.toNumeric(formFrontPart), 1) != null && houseInforOneDao.findByRentPriceAndIsStatus(stringOperateUtil.toNumeric(formFrontPart), 1).size() != 0)
                    houseInforAllList.addAll(getHouseInforAllUtil.GetHouseInforAll(houseInforOneDao.findByRentPriceAndIsStatus(stringOperateUtil.toNumeric(formFrontPart), 1)));

                if (houseInforOneDao.findBySquareMeterAndIsStatus(stringOperateUtil.toNumeric(formFrontPart), 1) != null && houseInforOneDao.findBySquareMeterAndIsStatus(stringOperateUtil.toNumeric(formFrontPart), 1).size() != 0)
                    houseInforAllList.addAll(getHouseInforAllUtil.GetHouseInforAll(houseInforOneDao.findBySquareMeterAndIsStatus(stringOperateUtil.toNumeric(formFrontPart), 1)));
            } else {
                if (houseInforOneDao.findByHouseTypeContainingAndIsStatus(formFrontPart, 1) != null && houseInforOneDao.findByHouseTypeContainingAndIsStatus(formFrontPart, 1).size() != 0)
                    houseInforAllList.addAll(getHouseInforAllUtil.GetHouseInforAll(houseInforOneDao.findByHouseTypeContainingAndIsStatus(formFrontPart, 1)));

                if (houseInforOneDao.findByCommunityContainingAndIsStatus(formFrontPart, 1) != null && houseInforOneDao.findByCommunityContainingAndIsStatus(formFrontPart, 1).size() != 0)
                    houseInforAllList.addAll(getHouseInforAllUtil.GetHouseInforAll(houseInforOneDao.findByCommunityContainingAndIsStatus(formFrontPart, 1)));

                if (houseInforOneDao.findByHouseAddressContainingAndIsStatus(formFrontPart, 1) != null && houseInforOneDao.findByHouseAddressContainingAndIsStatus(formFrontPart, 1).size() != 0)
                    houseInforAllList.addAll(getHouseInforAllUtil.GetHouseInforAll(houseInforOneDao.findByHouseAddressContainingAndIsStatus(formFrontPart, 1)));

                if (houseInforOneDao.findByRentTypeContainingAndIsStatus(formFrontPart, 1) != null && houseInforOneDao.findByRentTypeContainingAndIsStatus(formFrontPart, 1).size() != 0)
                    houseInforAllList.addAll(getHouseInforAllUtil.GetHouseInforAll(houseInforOneDao.findByRentTypeContainingAndIsStatus(formFrontPart, 1)));

                if (houseInforOneDao.findByPayTypeContainingAndIsStatus(formFrontPart, 1) != null && houseInforOneDao.findByPayTypeContainingAndIsStatus(formFrontPart, 1).size() != 0)
                    houseInforAllList.addAll(getHouseInforAllUtil.GetHouseInforAll(houseInforOneDao.findByPayTypeContainingAndIsStatus(formFrontPart, 1)));

                if (houseInforOneDao.findByAreaContainingAndIsStatus(formFrontPart, 1) != null && houseInforOneDao.findByAreaContainingAndIsStatus(formFrontPart, 1).size() != 0)
                    houseInforAllList.addAll(getHouseInforAllUtil.GetHouseInforAll(houseInforOneDao.findByAreaContainingAndIsStatus(formFrontPart, 1)));

                if (houseInforOneDao.findByMinRentDateContainingAndIsStatus(formFrontPart, 1) != null && houseInforOneDao.findByMinRentDateContainingAndIsStatus(formFrontPart, 1).size() != 0)
                    houseInforAllList.addAll(getHouseInforAllUtil.GetHouseInforAll(houseInforOneDao.findByMinRentDateContainingAndIsStatus(formFrontPart, 1)));

                if (houseInforOneDao.findByFloorContainingAndIsStatus(formFrontPart, 1) != null && houseInforOneDao.findByFloorContainingAndIsStatus(formFrontPart, 1).size() != 0)
                    houseInforAllList.addAll(getHouseInforAllUtil.GetHouseInforAll(houseInforOneDao.findByFloorContainingAndIsStatus(formFrontPart, 1)));

                if (houseInforOneDao.findByElevatorContainingAndIsStatus(formFrontPart, 1) != null && houseInforOneDao.findByElevatorContainingAndIsStatus(formFrontPart, 1).size() != 0)
                    houseInforAllList.addAll(getHouseInforAllUtil.GetHouseInforAll(houseInforOneDao.findByElevatorContainingAndIsStatus(formFrontPart, 1)));

                if (houseInforOneDao.findByDirectionContainingAndIsStatus(formFrontPart, 1) != null && houseInforOneDao.findByDirectionContainingAndIsStatus(formFrontPart, 1).size() != 0)
                    houseInforAllList.addAll(getHouseInforAllUtil.GetHouseInforAll(houseInforOneDao.findByDirectionContainingAndIsStatus(formFrontPart, 1)));

                if (houseInforOneDao.findBySubwayContainingAndIsStatus(formFrontPart, 1) != null && houseInforOneDao.findBySubwayContainingAndIsStatus(formFrontPart, 1).size() != 0)
                    houseInforAllList.addAll(getHouseInforAllUtil.GetHouseInforAll(houseInforOneDao.findBySubwayContainingAndIsStatus(formFrontPart, 1)));

                houseInforAllList.addAll(searchByFeature(formFrontPart));

                if(formFrontPart.length() == 9) {
                    String before = formFrontPart.substring(0, 4);
                    String after = formFrontPart.substring(5, 9);

                    if (stringOperateUtil.isNumeric(before.trim()) && stringOperateUtil.isNumeric(after.trim())) {
                        houseInforAllList.addAll(getHouseInforAllUtil.GetHouseInforAll(houseInforOneDao.findBetween(stringOperateUtil.toNumeric(before.trim()), stringOperateUtil.toNumeric(after.trim()), 1)));
                    }
                }
                if(formFrontPart.length() > 4 && formFrontPart.length() != 9) {
                    String underOrOn = formFrontPart.substring(0, 4);
                    if (stringOperateUtil.isNumeric(underOrOn.trim())) {
                        if (stringOperateUtil.toNumeric(underOrOn.trim()) == 1500) {
                            houseInforAllList.addAll(getHouseInforAllUtil.GetHouseInforAll(houseInforOneDao.findUnder(stringOperateUtil.toNumeric(underOrOn.trim()), 1)));
                            //houseInforAllList.addAll(getHouseInforAllUtil.GetHouseInforAll(houseInforOneDao.findOn(stringOperateUtil.toNumeric(underOrOn.trim()),1)));
                        }
                        if (stringOperateUtil.toNumeric(underOrOn.trim()) == 5000) {
                            //houseInforAllList.addAll(getHouseInforAllUtil.GetHouseInforAll(houseInforOneDao.findUnder(stringOperateUtil.toNumeric(underOrOn.trim()),1)));
                            houseInforAllList.addAll(getHouseInforAllUtil.GetHouseInforAll(houseInforOneDao.findOn(stringOperateUtil.toNumeric(underOrOn.trim()), 1)));
                        }
                    }
                }

            }
        }
        return houseInforAllList.stream().distinct().collect(Collectors.toList());
    }

    @Override
    public List<HouseInforAll> findHouseInforForThree(String selectValue, String regionOrSubway, String checkboxsValue) {
        List<HouseInforAll> houseInforAllList = new ArrayList<>();

        if (checkboxsValue.trim() == null || checkboxsValue.trim().length() == 0) {
            if ((selectValue.trim() == null || selectValue.trim().length() == 0) && (regionOrSubway.trim() == null || regionOrSubway.trim().length() == 0)) {
                houseInforAllList = houseInforAllService.findForHouseInforAll();
            } else if ((selectValue.trim() == null || selectValue.trim().length() == 0) && (regionOrSubway.trim() != null && regionOrSubway.trim().length() != 0)) {
                if (Character.isDigit(regionOrSubway.trim().charAt(0)))
                    houseInforAllList = searchBySubway(regionOrSubway.trim());
                else
                    houseInforAllList = searchByArea(regionOrSubway.trim());
            } else if ((selectValue.trim() != null && selectValue.trim().length() != 0) && (regionOrSubway.trim() == null || regionOrSubway.trim().length() == 0)) {
                houseInforAllList = findHouseInforAll(selectValue.trim());
            } else {
                if (Character.isDigit(regionOrSubway.trim().charAt(0)))
                    houseInforAllList = listForOverlapUtil.getOverlap(searchBySubway(regionOrSubway.trim()), findHouseInforAll(selectValue.trim()));
                else
                    houseInforAllList = listForOverlapUtil.getOverlap(searchByArea(regionOrSubway.trim()), findHouseInforAll(selectValue.trim()));
            }
        } else {
            if ((selectValue.trim() == null || selectValue.trim().length() == 0) && (regionOrSubway.trim() == null || regionOrSubway.trim().length() == 0)) {
                houseInforAllList = findHouseInforAll(checkboxsValue.trim());
            } else if ((selectValue.trim() == null || selectValue.trim().length() == 0) && (regionOrSubway.trim() != null && regionOrSubway.trim().length() != 0)) {
                if (Character.isDigit(regionOrSubway.trim().charAt(0)))
                    houseInforAllList = listForOverlapUtil.getOverlap(searchBySubway(regionOrSubway.trim()), findHouseInforAll(checkboxsValue.trim()));
                else
                    houseInforAllList = listForOverlapUtil.getOverlap(searchByArea(regionOrSubway.trim()), findHouseInforAll(checkboxsValue.trim()));
            } else if ((selectValue.trim() != null && selectValue.trim().length() != 0) && (regionOrSubway.trim() == null || regionOrSubway.trim().length() == 0)) {
                houseInforAllList = listForOverlapUtil.getOverlap(findHouseInforAll(selectValue.trim()), findHouseInforAll(checkboxsValue.trim()));
            } else {
                if (Character.isDigit(regionOrSubway.trim().charAt(0)))
                    houseInforAllList = listForOverlapUtil.getOverlap(
                            listForOverlapUtil.getOverlap(searchBySubway(regionOrSubway.trim()), findHouseInforAll(selectValue.trim())), findHouseInforAll(checkboxsValue.trim()));
                else
                    houseInforAllList = listForOverlapUtil.getOverlap(
                            listForOverlapUtil.getOverlap(searchByArea(regionOrSubway.trim()), findHouseInforAll(selectValue.trim())), findHouseInforAll(checkboxsValue.trim()));
            }
        }

        return houseInforAllList;
    }

    @Override
    public List<HouseInforAll> searchByFeature(String feature) {
        List<HouseInforAll> houseInforAllList = new ArrayList<>();

        for(int i = 0;i< stringOperateUtil.stringIncise(feature.trim()," ").length; i++) {
            for(HouseInforAll houseInforAll : houseInforAllService.findForHouseInforAll()) {
                for(String type : houseInforAll.getType()) {
                    if(type.trim().equals(stringOperateUtil.stringIncise(feature.trim(), " ")[i].trim())) {
                        System.out.println(houseInforAll);
                        houseInforAllList.add(houseInforAll);
                        break;
                    }
                }
            }
        }

        return houseInforAllList.stream().distinct().collect(Collectors.toList());
    }

    @Override
    public List<HouseInforAll> searchBySubway(String regionOrSubway) {
        return getHouseInforAllUtil.GetHouseInforAll(houseInforOneDao.findBySubwayContainingAndIsStatus(regionOrSubway,1));
    }

    @Override
    public List<HouseInforAll> searchByArea(String regionOrSubway) {
        return getHouseInforAllUtil.GetHouseInforAll(houseInforOneDao.findByAreaContainingAndIsStatus(regionOrSubway,1));
    }
}
